/**
	************************************************************
	************************************************************
	************************************************************
	*	文件名： 	main.c
	*
	*	作者： 		张继瑞
	*
	*	日期： 		2017-05-08
	*
	*	版本： 		V1.0
	*
	*	说明： 		接入onenet，上传数据和命令控制
	*
	*	修改记录：	
	************************************************************
	************************************************************
	************************************************************
**/

//单片机头文件
#include "stm32f10x.h"

//网络协议层
#include "onenet.h"

//网络设备
#include "esp8266.h"

//硬件驱动
#include "delay.h"
#include "beep.h"
#include "usart.h"
#include "i2c.h"
#include "sht20.h"
#include "light.h"
#include "lcd1602.h"
#include "led.h"
#include "hwtimer.h"
//C库
#include <string.h>
#include "stdio.h"
#include <stdlib.h>
/*
************************************************************
*	函数名称：	Hardware_Init
*
*	函数功能：	硬件初始化
*
*	入口参数：	无
*
*	返回参数：	无
*
*	说明：		初始化单片机功能以及外接设备
************************************************************
*/
void Hardware_Init(void)
{
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	//中断控制器分组设置

	Delay_Init();									//systick初始化
	
	Usart1_Init(115200);							//串口1，打印信息用
	LIGHT_Init();	
	Usart2_Init(115200);							//串口2，驱动ESP8266用
	Lcd1602_Init();	
	IIC_Init();										//IIC初始化
	Led_Init();
	Beep_Init();									//蜂鸣器初始化
	
	UsartPrintf(USART_DEBUG, " Hardware init OK\r\n");
	
}


unsigned int pd(){
		  unsigned char *dataPtr = NULL;
	    char buf[256];
	    memset(buf, 0, sizeof(buf));
	    //sprintf(buf, "GET http://api.heclouds.com/devices/%s/datastreams/%s HTTP/1.1\r\n api-key:%s\r\n Host:api.heclouds.com\r\n \r\n\r\n" , "702093014", "guangzhaoqiangdu","HqRvHnrk8Iu3pf8kLGWGmapWyOQ=");
			strcat(buf, "GET http://api.heclouds.com/devices/");
	    strcat(buf, "702093014");
	    strcat(buf, "/datastreams/");
	    strcat(buf, "guangzhaoqiangdu");
	    strcat(buf, " HTTP/1.1\r\n");
	    strcat(buf, "api-key:");
	    strcat(buf, "HqRvHnrk8Iu3pf8kLGWGmapWyOQ=");
	    strcat(buf, "\r\n");
    	strcat(buf, "Host:api.heclouds.com\r\n");
	    strcat(buf, "\r\n\r\n");
	    ESP8266_SendData((unsigned char *)buf, strlen(buf));
			dataPtr = ESP8266_GetIPD(500);
		if(dataPtr != NULL){
			//Lcd1602_DisString(0xC0, (char *)dataPtr);
			float data = atof((char *)dataPtr);
			//UsartPrintf(USART_DEBUG, (char *)dataPtr);
			if(data > 0.5){
				return 1;
			}
			else{
				return 0;
			}
		}
	   return 2;
}
/*
************************************************************
*	函数名称：	main
*
*	函数功能：	
*
*	入口参数：	无
*
*	返回参数：	0
*
*	说明：		
************************************************************
*/
int main(void)
{
  unsigned int c = 0;
	unsigned int Second = 0;
	char value[80];
	char value2[80];
	Hardware_Init();				//初始化外围硬件
	
	ESP8266_Init();					//初始化ESP8266
	
	//Beep_Set(BEEP_ON);				//鸣叫提示接入成功
	DelayXms(250);
	//Beep_Set(BEEP_OFF);
	Timer6_7_Init(TIM6, 1999, 35999);
	while(1)
	{
		SHT20_GetValue();
		sprintf(value, "T:%0.2f H:%0.2f", sht20_info.tempreture,sht20_info.humidity);
		Lcd1602_DisString(0x80, value);
		if(timer_info.timer6Out){
		timer_info.timer6Out = 0;
			Second++;
		}
  if(c == 0 || c==2){
	if(Second<7){
		    Led4_Set(LED_ON);
				Led5_Set(LED_OFF);
				Led6_Set(LED_OFF);
		Lcd1602_Clear(0xC0);
		//memset(value2, 0, sizeof(value2));
		sprintf(value2, "REDLED:%d", (7-Second));
		Lcd1602_DisString(0xC0, value2);
	}
	else if(Second>=7&&Second<9){
				Led4_Set(LED_OFF);
				 Led5_Set(LED_OFF);
				Led6_Set(LED_ON);
		Lcd1602_Clear(0xC0);
		//memset(value2, 0, sizeof(value2));
		sprintf(value2, "YELLOWLED:%d", (9-Second));
		Lcd1602_DisString(0xC0, value2);
			}
	else if(Second>=9&&Second<16){
				Led4_Set(LED_OFF);
				Led5_Set(LED_ON);
				Led6_Set(LED_OFF);
		Lcd1602_Clear(0xC0);
		//memset(value2, 0, sizeof(value2));
		sprintf(value2, "GREENLED:%d", (16-Second));
		Lcd1602_DisString(0xC0, value2);
			}
	else
		{
			Second = 0;
			c = pd();
	}
		}
	if(c == 1){
	if(Second<5){
		    Led4_Set(LED_ON);
				Led5_Set(LED_OFF);
				Led6_Set(LED_OFF);
		Lcd1602_Clear(0xC0);
		//memset(value2, 0, sizeof(value2));
		sprintf(value2, "REDLED:%d", (5-Second));
		Lcd1602_DisString(0xC0, value2);
	}
	else if(Second>=5&&Second<7){
				Led4_Set(LED_OFF);
				Led5_Set(LED_OFF);
				Led6_Set(LED_ON);
		Lcd1602_Clear(0xC0);
		//memset(value2, 0, sizeof(value2));
		sprintf(value2, "YELLOWLED:%d", (7-Second));
		Lcd1602_DisString(0xC0, value2);
			}
	else if(Second>=7&&Second<17){
				Led4_Set(LED_OFF);
				Led5_Set(LED_ON);
				Led6_Set(LED_OFF);
		Lcd1602_Clear(0xC0);
		//memset(value2, 0, sizeof(value2));
		sprintf(value2, "GREENLED:%d", (17-Second));
		Lcd1602_DisString(0xC0, value2);
			}
	else{
		Second = 0;
		c = pd();
	}
	}
	
		/*
		if(++timeCount >= 500)		//发送间隔5s
		{
			SHT20_GetValue();
			LIGHT_GetVoltag();
			sprintf(value,"value= %f",light_info.voltag);
		  Lcd1602_DisString(0x80, value);
		  Lcd1602_DisString(0xC0, "China Mobile");
			UsartPrintf(USART_DEBUG, "OneNet_SendData\r\n");
			
			OneNet_SendData();
			
			timeCount = 0;
			ESP8266_Clear();
		}
		
		dataPtr = ESP8266_GetIPD(0);
		if(dataPtr != NULL)
			OneNet_RevPro(dataPtr);
		
		DelayXms(10);
	*/
	
	}

}
